/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.actions.system;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.tiberiumlabs.lailaps.Mediator;
import org.tiberiumlabs.swing.DesktopToolkit;

/**
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class ExitAction extends AbstractAction implements WindowListener {

    private final Log LOG = LogFactory.getLog(ExitAction.class);
    private JWindow window;

    @Override
    public void actionPerformed(ActionEvent e) {
        performShutdown();
    }

    @Override
    public void windowClosing(WindowEvent e) {
        performShutdown();
    }

    private void performShutdown() {
        if (SwingUtilities.isEventDispatchThread()) {
            showProgress();
            Thread thread = new Thread("Lailaps Shutdown") {
                @Override
                public void run() {
                    shutdown();
                }
            };
            thread.start();
        }
        else {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    showProgress();
                }
            });
            shutdown();
        }
    }
    
    private void showProgress() {
        JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL);
        progressBar.setBorderPainted(true);
        progressBar.setIndeterminate(true);
        progressBar.setStringPainted(true);
        progressBar.setString("Shutting down...");
        progressBar.setFont(new Font(Font.MONOSPACED, Font.BOLD, 12));

        JPanel content = new JPanel();
        content.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(new Color(70, 80, 90)), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
        content.add(progressBar);

        window = new JWindow();
        window.setContentPane(content);
        window.pack();
        if (window.getWidth() < 400) {
            window.setSize(400, window.getHeight());
        }
        window.setLocation(DesktopToolkit.getWindowLocation(window.getSize()));
        window.setVisible(true);
    }

    private void shutdown() {
        try {

            LOG.info("Exiting...");
            Mediator.getMediator().getGuiBuilder().getMainWindow().dispose();

        } catch (Exception ex) {
            LOG.error("Exception while shutting down", ex);
        } finally {
            if (window != null) {
                window.dispose();
                window = null;
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc=" implementing empty window listener ">
    @Override
    public void windowOpened(WindowEvent e) {
    }
    @Override
    public void windowClosed(WindowEvent e) {
    }
    @Override
    public void windowIconified(WindowEvent e) {
    }
    @Override
    public void windowDeiconified(WindowEvent e) {
    }
    @Override
    public void windowActivated(WindowEvent e) {
    }
    @Override
    public void windowDeactivated(WindowEvent e) {
    }
    // </editor-fold>

}
